/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import system.*;

/**
 *
 * @author Thommie
 */
public class UpperPanel extends JPanel implements ActionListener {

    private FlowLayout flowLayout;
    private Checkbox favBox;
    private JLabel grades;
    private JLabel favorite;
    private BoxLayout boxLeft;
    private BoxLayout boxBot;
    private JScrollPane scrollPane;
    private JScrollPane scrollPane2;
    private URL url = null;
    private Image uImg;
    private javax.swing.JTextArea movieInfo;
    private javax.swing.JTextArea plot;
    private JButton button;
    private String test;
    private JPanel right;
    private JPanel center;
    private JPanel gradePanel;
    private JPanel favPanel;
    //private JPanel innerLeft;
    private JPanel innerTop;
    private JPanel innerC;
    private JPanel innerBot;
    private ImagePanel iP;
    private JRadioButton rB1;
    private JRadioButton rB2;
    private JRadioButton rB3;
    private JRadioButton rB4;
    private JRadioButton rB5;
    private ButtonGroup bG;
    private MovieHandler movies;
    private UserHandler users;
    private GradeHandler gh;
    private int preRB = 0;
    private LeftPanel leftPanel;

    public UpperPanel(MovieHandler movies, UserHandler users, GradeHandler gh) {
        this.movies = movies;
        this.users = users;
        this.gh = gh;
        //boxBot = new BoxLayout(bot, BoxLayout.X_AXIS);
        //boxLeft = new BoxLayout(left, BoxLayout.X_AXIS);
        gradePanel = new JPanel();
        favPanel = new JPanel();
        flowLayout = new FlowLayout();
        favBox = new Checkbox();
        grades = new JLabel("Grade: ");
        favorite = new JLabel("Favorite: ");
        bG = new ButtonGroup();
        rB1 = new JRadioButton("1");
        rB2 = new JRadioButton("2");
        rB3 = new JRadioButton("3");
        rB4 = new JRadioButton("4");
        rB5 = new JRadioButton("5");
        iP = new ImagePanel();
        bG.add(rB1);
        bG.add(rB2);
        bG.add(rB3);
        bG.add(rB4);
        bG.add(rB5);


        Dimension dimens = new Dimension(200, 300);
        iP.setPreferredSize(dimens);
        //button = new JButton("post");
        right = new JPanel();
        center = new JPanel();

        center.setLayout(new BorderLayout());
        //center.setLayout(new GridLayout(2, 1, 0, 5));
        //innerCenter = new JPanel();
        innerTop = new JPanel();
        innerC = new JPanel();
        innerBot = new JPanel();
        innerBot.setLayout(flowLayout);
        this.setLayout(new BorderLayout());

        movieInfo = new javax.swing.JTextArea();
        plot = new javax.swing.JTextArea();

        this.add(right, BorderLayout.EAST);
        this.add(center, BorderLayout.CENTER);

        center.add(innerTop, BorderLayout.NORTH);
        center.add(innerC, BorderLayout.CENTER);
        center.add(innerBot, BorderLayout.SOUTH);

        innerTop.setLayout(new FlowLayout(FlowLayout.LEFT));
        innerC.setLayout(new FlowLayout(FlowLayout.LEFT));
        innerTop.add(movieInfo);
        innerC.add(plot);
        innerTop.setBorder(BorderFactory.createEmptyBorder(20, 40, 0, 0)); // uppe,vänster,nere,höger
        innerC.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0)); // uppe,vänster,nere,höger


        movieInfo.setSelectionColor(Color.GREEN);
        plot.setColumns(30);
        plot.setRows(6);
        plot.setSelectionColor(Color.GREEN);

        //Right
        right.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 20)); // uppe,vänster,nere,höger
        right.add(iP);

        movieInfo.setColumns(27);
        movieInfo.setRows(11);
        movieInfo.setEditable(false);
        movieInfo.setTabSize(4);

        scrollPane = new JScrollPane(movieInfo);
        scrollPane2 = new JScrollPane(plot);
        innerTop.add(scrollPane);
        innerC.add(scrollPane2);
        plot.setLineWrap(true);
        plot.setEditable(false);

        innerBot.setLayout(new BorderLayout());
        innerBot.add(gradePanel, BorderLayout.CENTER); //Hear'd u like panels, so we put a panel in  ur panel.
        innerBot.add(favPanel, BorderLayout.EAST);//Hear'd u like panels, so we put a panel in ur panel.

        gradePanel.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0)); // uppe,vänster,nere,höger
        gradePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        gradePanel.add(grades);
        gradePanel.add(rB1);
        gradePanel.add(rB2);
        gradePanel.add(rB3);
        gradePanel.add(rB4);
        gradePanel.add(rB5);

        favPanel.add(favorite);
        favPanel.add(favBox);

        favPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 80)); // uppe,vänster,nere,höger


        // textPostArea.setSelectedTextColor(Color.GREEN);
        // Add the scroll pane to the panel.

        //  button.addActionListener(this);

        RadioListener listener = new RadioListener();
        CheckBoxListener checkListener = new CheckBoxListener();
        rB1.addActionListener(listener);
        rB2.addActionListener(listener);
        rB3.addActionListener(listener);
        rB4.addActionListener(listener);
        rB5.addActionListener(listener);
        favBox.addItemListener(checkListener);


    }

    public void setLeftPanel(LeftPanel leftPanel) {
        this.leftPanel = leftPanel;
    }

    public void updateRadioButtons() {
        try {
            int buttonNbr = 0;
            if (movies.getSelectedMovie() != null && gh.getAGrade(movies.getSelectedMovie().getMovieID(), users.getCurrentUser().getUserID()).getGrade() >= 0) {
                buttonNbr = (int) gh.getAGrade(movies.getSelectedMovie().getMovieID(), users.getCurrentUser().getUserID()).getGrade();
            }
            switch (buttonNbr) {
                case 1:
                    rB1.setSelected(true);
                    preRB = 1;
                    break;
                case 2:
                    rB2.setSelected(true);
                    preRB = 2;
                    break;
                case 3:
                    rB3.setSelected(true);
                    preRB = 3;
                    break;
                case 4:
                    rB4.setSelected(true);
                    preRB = 4;
                    break;
                case 5:
                    rB5.setSelected(true);
                    preRB = 5;
                    break;
                default:
                    bG.clearSelection();
                    break;
            }
        } catch (Exception e) {
            System.out.println("error in updateRadioButton: ");
            System.out.println(e);

            bG.clearSelection();
        }
    }

    public JTextArea getMovieInfo() {
        return movieInfo;
    }

    public JTextArea getPlot() {
        return plot;
    }

    public void updateIMG(String u) {
        iP.updateImage(u);
    }

    public Checkbox getFavBox() {
        return favBox;
    }

    public void actionPerformed(ActionEvent e) {
    }

    /** Listens to the radio buttons. */
    private class RadioListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("1")) {
                gh.gradeAMovie(movies.getSelectedMovie().getMovieID(), users.getCurrentUser().getUserID(), 1);
            } else if (e.getActionCommand().equals("2")) {
                gh.gradeAMovie(movies.getSelectedMovie().getMovieID(), users.getCurrentUser().getUserID(), 2);
            } else if (e.getActionCommand().equals("3")) {
                gh.gradeAMovie(movies.getSelectedMovie().getMovieID(), users.getCurrentUser().getUserID(), 3);
            } else if (e.getActionCommand().equals("4")) {
                gh.gradeAMovie(movies.getSelectedMovie().getMovieID(), users.getCurrentUser().getUserID(), 4);
            } else if (e.getActionCommand().equals("5")) {
                gh.gradeAMovie(movies.getSelectedMovie().getMovieID(), users.getCurrentUser().getUserID(), 5);
            }

            updateRadioButtons();
            getMovieInfo().setText(movies.getMovieInfo(movies.getSelectedMovie().getMovieID()));
            leftPanel.updateTop10List();

        }
    }

    private class CheckBoxListener implements ItemListener {

        public void itemStateChanged(ItemEvent e) {
            System.out.println(e);
            gh.setFavourite(movies.getSelectedMovie().getMovieID(), users.getCurrentUser().getUserID(), favBox.getState());
            if (favBox.getState()) {
                
                gh.setLastFavourited(movies.getSelectedMovie().getTitle());
                leftPanel.addFavoruteAndUpdateList(movies.getSelectedMovie().getTitle());
            } else if (!favBox.getState()) {
                gh.setLastFavourited(movies.getSelectedMovie().getTitle());
                leftPanel.removeFavoruteAndUpdateList(movies.getSelectedMovie().getTitle());
            }

        }
    }
}
